<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="简介jQuery 是目前使用最广泛的 JavaScript 函数库 对于网页开发者来说，学会 jQuery 是必要的。因为它让你了解业界最通用的技术，为将来学习更高级的库打下基础，并且确实可以很轻松地做出许多复杂的效果。 jQuery 是一个高效、精简并且功能丰富的 JavaScript 工具库。它提供的 API 易于使用且兼容众多浏览器，这让诸如 HTML 文档遍历和操作、事件处理、动画和 Aj">
<meta property="og:type" content="article">
<meta property="og:title" content="浅析jQuery设计思想">
<meta property="og:url" content="http://example.com/2021/09/12/01/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="简介jQuery 是目前使用最广泛的 JavaScript 函数库 对于网页开发者来说，学会 jQuery 是必要的。因为它让你了解业界最通用的技术，为将来学习更高级的库打下基础，并且确实可以很轻松地做出许多复杂的效果。 jQuery 是一个高效、精简并且功能丰富的 JavaScript 工具库。它提供的 API 易于使用且兼容众多浏览器，这让诸如 HTML 文档遍历和操作、事件处理、动画和 Aj">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-09-12T06:50:01.000Z">
<meta property="article:modified_time" content="2021-09-12T06:59:54.545Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="js">
<meta property="article:tag" content="jquery">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/09/12/01/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2021/09/12/01/","path":"2021/09/12/01/","title":"浅析jQuery设计思想"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>浅析jQuery设计思想 | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%80%E4%BB%8B"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E7%BD%91%E9%A1%B5%E5%85%83%E7%B4%A0"><span class="nav-number">2.</span> <span class="nav-text">选择网页元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B2%BE%E7%A1%AE%E9%80%89%E6%8B%A9%E8%8C%83%E5%9B%B4"><span class="nav-number">3.</span> <span class="nav-text">精确选择范围</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%8D%E5%88%B6%E3%80%81%E5%88%A0%E9%99%A4%E3%80%81%E5%88%9B%E5%BB%BA%E5%85%83%E7%B4%A0"><span class="nav-number">4.</span> <span class="nav-text">复制、删除、创建元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%93%BE%E5%BC%8F%E6%93%8D%E4%BD%9C"><span class="nav-number">5.</span> <span class="nav-text">链式操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E5%85%83%E7%B4%A0"><span class="nav-number">6.</span> <span class="nav-text">移动元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%8D%E8%BD%BD"><span class="nav-number">7.</span> <span class="nav-text">重载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#getter-setter"><span class="nav-number">8.</span> <span class="nav-text">getter&#x2F;setter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%82%E9%85%8D%E5%99%A8"><span class="nav-number">9.</span> <span class="nav-text">适配器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%94%A8%E9%97%AD%E5%8C%85%E9%9A%90%E8%97%8F%E7%BB%86%E8%8A%82"><span class="nav-number">10.</span> <span class="nav-text">用闭包隐藏细节</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/12/01/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浅析jQuery设计思想
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-09-12 14:50:01" itemprop="dateCreated datePublished" datetime="2021-09-12T14:50:01+08:00">2021-09-12</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p>jQuery 是目前使用最广泛的 JavaScript 函数库</p>
<p>对于网页开发者来说，学会 jQuery 是必要的。因为它让你了解业界最通用的技术，为将来学习更高级的库打下基础，并且确实可以很轻松地做出许多复杂的效果。</p>
<p>jQuery 是一个高效、精简并且功能丰富的 JavaScript 工具库。它提供的 API 易于使用且兼容众多浏览器，这让诸如 HTML 文档遍历和操作、事件处理、动画和 Ajax 操作更加简单。</p>
<p>jQuery 的基本设计思想就是，<strong>选择某个网页元素，然后对其进行某种操作</strong></p>
<p>jQuery 的实质就是一个<strong>封装的 DOM 库</strong>。</p>
<blockquote>
<p>仿 jQuery 手写了一些简单的 api，有兴趣可以参考一下 <a target="_blank" rel="noopener" href="https://github.com/Drwna/dom-2">简易jQuery</a></p>
</blockquote>
<h3 id="选择网页元素"><a href="#选择网页元素" class="headerlink" title="选择网页元素"></a>选择网页元素</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//jQuery中:jQuery() = $()</span></span><br><span class="line"><span class="comment">//将一个选择表达式，放进构造函数$()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//选择表达式可以是CSS选择器</span></span><br><span class="line">$(<span class="variable language_">document</span>) <span class="comment">//选择整个文档对象</span></span><br><span class="line">$(<span class="string">&#x27;#myId&#x27;</span>) <span class="comment">//选择ID为myId的网页元素</span></span><br><span class="line">$(<span class="string">&#x27;div.myClass&#x27;</span>) <span class="comment">// 选择class为myClass的div元素</span></span><br><span class="line">$(<span class="string">&#x27;input[name=first]&#x27;</span>) <span class="comment">// 选择name属性等于first的input元素</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//也可以是jQuery特有的表达式</span></span><br><span class="line">$(<span class="string">&#x27;a:first&#x27;</span>) <span class="comment">//选择网页中第一个a元素</span></span><br><span class="line">$(<span class="string">&#x27;tr:odd&#x27;</span>) <span class="comment">//选择表格的奇数行</span></span><br><span class="line">$(<span class="string">&#x27;#myForm :input&#x27;</span>) <span class="comment">// 选择表单中的input元素</span></span><br><span class="line">$(<span class="string">&#x27;div:visible&#x27;</span>) <span class="comment">//选择可见的div元素</span></span><br><span class="line">$(<span class="string">&#x27;div:gt(2)&#x27;</span>) <span class="comment">// 选择所有的div元素，除了前三个</span></span><br><span class="line">$(<span class="string">&#x27;div:animated&#x27;</span>) <span class="comment">// 选择当前处于动画状态的div元素</span></span><br></pre></td></tr></table></figure>



<h3 id="精确选择范围"><a href="#精确选择范围" class="headerlink" title="精确选择范围"></a>精确选择范围</h3><p>思想：提供各种强大的过滤器，对结果集进行筛选，缩小选择结果</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">has</span>(<span class="string">&#x27;p&#x27;</span>); <span class="comment">// 选择包含p元素的div元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">not</span>(<span class="string">&#x27;.myClass&#x27;</span>); <span class="comment">//选择class不等于myClass的div元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">filter</span>(<span class="string">&#x27;.myClass&#x27;</span>); <span class="comment">//选择class等于myClass的div元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">first</span>(); <span class="comment">//选择第1个div元素</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//也可以从当前元素出发，移动到附近的相关元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">next</span>(<span class="string">&#x27;p&#x27;</span>); <span class="comment">//选择div元素后面的第一个p元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">parent</span>(); <span class="comment">//选择div元素的父元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">closest</span>(<span class="string">&#x27;form&#x27;</span>); <span class="comment">//选择离div最近的那个form父元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">children</span>(); <span class="comment">//选择div的所有子元素</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">siblings</span>(); <span class="comment">//选择div的同级元素</span></span><br></pre></td></tr></table></figure>



<h3 id="复制、删除、创建元素"><a href="#复制、删除、创建元素" class="headerlink" title="复制、删除、创建元素"></a>复制、删除、创建元素</h3><ul>
<li>复制元素使用 <code>.clone()</code></li>
<li>删除元素使用 <code>.remove()</code> 和 <code>.detach()</code></li>
</ul>
<blockquote>
<p>两者区别：前者不保留被删除元素的事件，后者保留，有利于重新插入文档时使用</p>
</blockquote>
<ul>
<li>清空元素内容（但是不删除该元素）使用 <code>.empty()</code></li>
<li>创建元素只要把新元素直接传入jQuery的构造函数就行了</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;&lt;p&gt;Hello&lt;/p&gt;&#x27;</span>);</span><br><span class="line">$(<span class="string">&#x27;&lt;li class=&quot;new&quot;&gt;new list item&lt;/li&gt;&#x27;</span>);</span><br><span class="line">$(<span class="string">&#x27;ul&#x27;</span>).<span class="title function_">append</span>(<span class="string">&#x27;&lt;li&gt;list item&lt;/li&gt;&#x27;</span>);</span><br></pre></td></tr></table></figure>



<h3 id="链式操作"><a href="#链式操作" class="headerlink" title="链式操作"></a>链式操作</h3><p>思想：最终选中网页元素以后，可以对它进行一系列操作，并且所有操作可以连接在一起，以链条的形式写出来</p>
<p>这样以点连起来的连续调用就称为链式调用，它的设计原理是<strong>把jQuery中的函数返回为这个对象，然后这个对象又包含jQuery的所有方法，这样就可以实现链式调用，十分方便</strong>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">find</span>(<span class="string">&#x27;h3&#x27;</span>).<span class="title function_">eq</span>(<span class="number">2</span>).<span class="title function_">html</span>(<span class="string">&#x27;Hello&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//分解开，可以这么理解</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>) <span class="comment">//找到div元素</span></span><br><span class="line">　　　.<span class="title function_">find</span>(<span class="string">&#x27;h3&#x27;</span>) <span class="comment">//选择其中的h3元素</span></span><br><span class="line">　　　.<span class="title function_">eq</span>(<span class="number">2</span>) <span class="comment">//选择第3个h3元素</span></span><br><span class="line">　　　.<span class="title function_">html</span>(<span class="string">&#x27;Hello&#x27;</span>); <span class="comment">//将它的内容改为Hello</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//jQuery还提供了.end()方法，使得结果可以后退一步</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">　　　.<span class="title function_">find</span>(<span class="string">&#x27;h3&#x27;</span>)</span><br><span class="line">　　　.<span class="title function_">eq</span>(<span class="number">2</span>)</span><br><span class="line">　　　.<span class="title function_">html</span>(<span class="string">&#x27;Hello&#x27;</span>)</span><br><span class="line">　　　.<span class="title function_">end</span>() <span class="comment">//退回到选中所有的h3元素的那一步</span></span><br><span class="line">　　　.<span class="title function_">eq</span>(<span class="number">0</span>) <span class="comment">//选中第一个h3元素</span></span><br><span class="line">　　　.<span class="title function_">html</span>(<span class="string">&#x27;World&#x27;</span>); <span class="comment">//将它的内容改为World</span></span><br></pre></td></tr></table></figure>



<h3 id="移动元素"><a href="#移动元素" class="headerlink" title="移动元素"></a>移动元素</h3><p>思想：jQuery提供两组方法，来操作元素在网页中的位置移动。一组方法是直接移动该元素，另一组方法是移动其他元素，使得目标元素达到我们想要的位置</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一种方法是使用.insertAfter()</span></span><br><span class="line"><span class="comment">//把div元素移动p元素后面</span></span><br><span class="line">$(<span class="string">&#x27;div&#x27;</span>).<span class="title function_">insertAfter</span>($(<span class="string">&#x27;p&#x27;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//第二种方法是使用.after()</span></span><br><span class="line"><span class="comment">//把p元素加到div元素前面</span></span><br><span class="line">$(<span class="string">&#x27;p&#x27;</span>).<span class="title function_">after</span>($(<span class="string">&#x27;div&#x27;</span>));</span><br></pre></td></tr></table></figure>

<blockquote>
<p>二者区别：返回的元素不一样。第一种方法返回div元素，第二种方法返回p元素。</p>
</blockquote>
<p>类似操作方法共4对</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">　.<span class="title function_">insertAfter</span>()和.<span class="title function_">after</span>() <span class="comment">//在现存元素的外部，从后面插入元素</span></span><br><span class="line">　.<span class="title function_">insertBefore</span>()和.<span class="title function_">before</span>() <span class="comment">//在现存元素的外部，从前面插入元素</span></span><br><span class="line">　.<span class="title function_">appendTo</span>()和.<span class="title function_">append</span>() <span class="comment">//在现存元素的内部，从后面插入元素</span></span><br><span class="line">　.<span class="title function_">prependTo</span>()和.<span class="title function_">prepend</span>() <span class="comment">//在现存元素的内部，从前面插入元素</span></span><br></pre></td></tr></table></figure>



<h3 id="重载"><a href="#重载" class="headerlink" title="重载"></a>重载</h3><p>jQuery中，构造函数$()<strong>支持多种参数</strong></p>
<ul>
<li>.attr(name，value)和.attr(name)：两个参数是设置属性，一个参数是获取属性值</li>
<li>.text()和.text(string)：无参数是获取文本，一个参数是写入文本</li>
<li>.html()和.html(string)：无参数是获取html元素，一个参数是写入html元素</li>
</ul>
<p>结合下面这段代码，参数可以是选择器、数组或一段代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">jQuery</span> = <span class="keyword">function</span>(<span class="params">selectorOrArrayOrTemplate</span>)&#123;</span><br><span class="line">  <span class="keyword">let</span> elements</span><br><span class="line">  <span class="keyword">if</span>(<span class="keyword">typeof</span> selectorOrArrayOrTemplate === <span class="string">&#x27;string&#x27;</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(selectorOrArrayOrTemplate[<span class="number">0</span>] === <span class="string">&#x27;&lt;&#x27;</span>)&#123;</span><br><span class="line">      <span class="comment">// 创建 div</span></span><br><span class="line">      elements=[<span class="title function_">createElement</span>(selectorOrArrayOrTemplate)]</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">      <span class="comment">// 查找 div</span></span><br><span class="line">      elements = <span class="variable language_">document</span>.<span class="title function_">querySelectorAll</span>(selectorOrArrayOrTemplate)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(selectorOrArrayOrTemplate <span class="keyword">instanceof</span> <span class="title class_">Array</span>)&#123;</span><br><span class="line">    elements = selectorOrArrayOrTemplate</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//举例</span></span><br><span class="line">  $(<span class="string">&#x27;div&#x27;</span>)</span><br><span class="line">  $(<span class="string">&#x27;&lt;div&gt;hello&lt;/div&gt;&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    </span><br></pre></td></tr></table></figure>



<h3 id="getter-setter"><a href="#getter-setter" class="headerlink" title="getter/setter"></a>getter/setter</h3><p>思想：使用同一个函数，来完成取值（getter）和赋值（setter），到底是取值还是赋值，由函数的参数决定。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;h1&#x27;</span>).<span class="title function_">html</span>(); <span class="comment">//html()没有参数，表示取出h1的值</span></span><br><span class="line">$(<span class="string">&#x27;h1&#x27;</span>).<span class="title function_">html</span>(<span class="string">&#x27;Hello&#x27;</span>); <span class="comment">//html()有参数Hello，表示对h1进行赋值</span></span><br></pre></td></tr></table></figure>

<p>常见的取值和赋值函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">.<span class="title function_">html</span>() 取出或设置html内容</span><br><span class="line">.<span class="title function_">text</span>() 取出或设置text内容</span><br><span class="line">.<span class="title function_">attr</span>() 取出或设置某个属性的值</span><br><span class="line">.<span class="title function_">width</span>() 取出或设置某个元素的宽度</span><br><span class="line">.<span class="title function_">height</span>() 取出或设置某个元素的高度</span><br><span class="line">.<span class="title function_">val</span>() 取出某个表单元素的值</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如果结果集包含多个元素，那么赋值的时候，将对其中所有的元素赋值。<br>取值的时候，则是只取出第一个元素的值（.text()例外，它取出所有元素的text内容）</p>
</blockquote>
<h3 id="适配器"><a href="#适配器" class="headerlink" title="适配器"></a>适配器</h3><p>jQuery针对不同浏览器使用不同代码，也就是兼容各个浏览器。</p>
<h3 id="用闭包隐藏细节"><a href="#用闭包隐藏细节" class="headerlink" title="用闭包隐藏细节"></a>用闭包隐藏细节</h3><p>思想：浏览器必须通过内部的函数来操作外部参数，如用get()或find()来操作elements，可以隐藏细节。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">$</span> = <span class="variable language_">window</span>.<span class="property">jQuery</span> = <span class="keyword">function</span>(<span class="params">selectorOrArrayOrTemplate</span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> elements;</span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">typeof</span> selectorOrArrayOrTemplate === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (selectorOrArrayOrTemplate[<span class="number">0</span>] === <span class="string">&quot;&lt;&quot;</span>) &#123;</span><br><span class="line">      <span class="comment">// 创建 div</span></span><br><span class="line">      elements = [<span class="title function_">createElement</span>(selectorOrArrayOrTemplate)];</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">// 查找 div</span></span><br><span class="line">      elements = <span class="variable language_">document</span>.<span class="title function_">querySelectorAll</span>(selectorOrArrayOrTemplate);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (selectorOrArrayOrTemplate <span class="keyword">instanceof</span> <span class="title class_">Array</span>) &#123;</span><br><span class="line">    elements = selectorOrArrayOrTemplate;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">function</span> <span class="title function_">createElement</span>(<span class="params">string</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> container = <span class="variable language_">document</span>.<span class="title function_">createElement</span>(<span class="string">&quot;template&quot;</span>);</span><br><span class="line">    container.<span class="property">innerHTML</span> = string.<span class="title function_">trim</span>();</span><br><span class="line">    <span class="keyword">return</span> container.<span class="property">content</span>.<span class="property">firstChild</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  jQuery.<span class="property">fn</span> = jQuery.<span class="property"><span class="keyword">prototype</span></span> = &#123;</span><br><span class="line">    <span class="attr">constructor</span>: jQuery,</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">index</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">elements</span>[index];</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">find</span>(<span class="params">selector</span>) &#123;</span><br><span class="line">      <span class="keyword">let</span> array = [];</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">this</span>.<span class="property">elements</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        <span class="keyword">const</span> elements2 = <span class="title class_">Array</span>.<span class="title function_">from</span>(<span class="variable language_">this</span>.<span class="property">elements</span>[i].<span class="title function_">querySelectorAll</span>(selector));</span><br><span class="line">        array = array.<span class="title function_">concat</span>(<span class="variable language_">this</span>.<span class="property">elements2</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      array.<span class="property">oldApi</span> = <span class="variable language_">this</span>; <span class="comment">// this 就是 旧 api</span></span><br><span class="line">      <span class="keyword">return</span> <span class="title function_">jQuery</span>(array);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>参考自：</p>
<p><a target="_blank" rel="noopener" href="http://www.ruanyifeng.com/blog/2011/07/jquery_fundamentals.html">jQuery设计思想</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/js/" rel="tag"><i class="fa fa-tag"></i> js</a>
              <a href="/tags/jquery/" rel="tag"><i class="fa fa-tag"></i> jquery</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/12/56/" rel="prev" title="JS函数">
                  <i class="fa fa-chevron-left"></i> JS函数
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/17/37/" rel="next" title="JS对象分类">
                  JS对象分类 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
